#!/usr/bin/env node
// 加上这一行 说明这个文件是一个可执行文件  
const commander = require('commander'); // 命令行工具 参数解析
const inquirer = require('inquirer'); // 定义用户交互行为
const fs= require('fs')
const initJSON = {
    "name": "cli-1",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "bin": {
      "cli-12a": "./index.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
      "commander": "^7.2.0",
      "inquirer": "^8.1.1"
    }
  }
//   cli init -y
//   cli create my-app
commander
    .command('init')
    .option('-y')
    .action((option) => {
        if(option.y) {
            fs.writeFileSync('package2.json',JSON.stringify(initJSON))
        } else {
            inquirer.prompt([
                    {
                        type: "input",
                        name: "packageName",
                        message: "请输入 name"
                    },
                    {
                        type: "input",
                        name: "description",
                        message: "请输入 description"
                    },
                    {
                        type: "input",
                        name: "author",
                        message: "请输入 author"
                    }
                ]).then(res=> {
                    fs.writeFileSync('package2.json',JSON.stringify(res))
                })
        }
        
    })
// 解析命令行参数  不加不会触发commander
commander.parse(process.argv)